# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView

from Forecast_Call.packaging import create_task, create_seats_group, create_callers_group, start_task_test
from Forecast_Call.tasks import test
from utils.open_files import reading_file
from utils.aliyun_file_operations import aliyun_files_uploading,aliyun_files_download

# 开始任务
class Start_Task_View(APIView):
    def post(self,request):
        task_id = request.data.get('task_id','')
        account_tag = request.data.get('account_tag','')
        try:
            res = start_task_test(task_id=task_id,account_tag=account_tag)
            return Response(res)
        except Exception as e:
            return Response({"code":500,"msg":"系统繁忙","error":str(e)})

# 语音文件上传/下载
class Files_Test_View(APIView):
    def post(self,request):
        files = request.FILES.get('files','')
        aliyun_files_uploading(files)
        return Response({"code":200,"msg":"OK"})
    def get(self,request):
        aliyun_files_download('images.jpg')
        return Response({"code": 200, "msg": "OK"})

# 创建任务
class Create_Task_Test_View(APIView):
    def get(self,request):
        pass
    def post(self,request):
        task_name = request.data.get('task_name', '')
        call_list = request.FILES.get('call_list', '')
        call_way = request.data.get('call_way', '')
        queue_id = request.data.get('queue_id', '')
        priority = request.data.get('priority', '')
        start_time = request.data.get('start_time', '')
        end_time = request.data.get('end_time', '')
        callers_group = request.data.get('callers_group', '')
        template = request.data.get('template', '')
        concurrence = request.data.get('concurrence', '')
        status = request.data.get('status', '')
        remark = request.data.get('remark', '')
        res = create_task(task_name=task_name,call_list=reading_file(call_list),call_way=call_way,queue_id=queue_id,priority=priority,start_time=start_time,end_time=end_time,callers_group=callers_group,template=template,concurrence=concurrence,status=status,remark=remark)
        return Response(res)
    def put(self,request):
        pass
    def delete(self,request):
        pass

# 创建班组
class Create_Seats_Group_View(APIView):
    def get(self,request):
        pass
    def post(self,request):
        group_name = request.data.get('group_name','')
        group_leader = request.data.get('group_leader','')
        rule = request.data.get('rule','')
        status = request.data.get('status','')
        remark = request.data.get('remark','')
        seats_list = request.data.get('seats_list','')
        account_tag = request.data.get('account_tag','')
        res = create_seats_group(group_name=group_name,group_leader=group_leader,rule=rule,status=status,remark=remark,seats_list=seats_list,account_tag=account_tag)
        return Response(res)
    def put(self,request):
        pass
    def delete(self,request):
        pass

# 创建主叫分组
class Create_Callers_Group_View(APIView):
    def get(self,request):
        pass
    def post(self,request):
        group_name = request.data.get('group_name','')
        strategy = request.data.get('strategy','')
        usercli = request.FILES.get('usercli','')
        res = create_callers_group(group_name=group_name,strategy=strategy,usercli=reading_file(usercli))
        return Response(res)
    def put(self,request):
        pass
    def delete(self,request):
        pass

class Test_View(APIView):

    def get(self,request):
        test.delay()
        return Response({"code":200})